package com.wxdc.nmgyt.nmgytwydc.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.wxdc.nmgyt.nmgytwydc.R;
import com.wxdc.nmgyt.nmgytwydc.model.CbYfInfoBean;
import com.wxdc.nmgyt.nmgytwydc.model.YfCodeBean;

import java.util.List;

/**
 * Created by Administrator on 2018/7/18.
 */

public class YfInfo_RecyclerViewAdapter extends RecyclerView.Adapter<YfInfo_RecyclerViewAdapter.MyViewHolder> implements View.OnClickListener,View.OnLongClickListener{
    private Context mContext;
    private List<YfCodeBean> yfNames;
    private RecycleViewOnItemClickListener mItemClickListener;
    private RecycleViewOnItemLongClickListener mItemLongClickListener;
    @Override
    public void onClick(View v) {
        if (mItemClickListener != null) {
            mItemClickListener.onItemClick((Integer) v.getTag());
        }
    }

    @Override
    public boolean onLongClick(View v) {
        if (mItemLongClickListener != null) {
            mItemLongClickListener.onItemLongClick((Integer) v.getTag());
            return true;
        }
        return false;
    }

    public interface  RecycleViewOnItemClickListener{
        void onItemClick(int position);
    }
    public interface RecycleViewOnItemLongClickListener{
        void onItemLongClick(int position);
    }
    //--------------------------------------------------------------------
    public YfInfo_RecyclerViewAdapter(Context context, List<YfCodeBean> yfNames) {
        mContext = context;
        this.yfNames = yfNames;
    }
    //------------------------------------------------------------------------------------
    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(mContext);
        View itemView = inflater.inflate(R.layout.list_item_yf, parent, false);
        itemView.setOnClickListener(this);
        itemView.setOnLongClickListener(this);
        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        holder.itemView.setTag(position);
        holder.bind(yfNames.get(position).getYfName());
    }

    @Override
    public int getItemCount() {
        return yfNames == null ? 0 : yfNames.size();
    }
    //------------------------------------------------------------------------------------
    class MyViewHolder extends RecyclerView.ViewHolder {
        private TextView mYfNameTextView;
        public MyViewHolder(View itemView) {
            super(itemView);
            mYfNameTextView = itemView.findViewById(R.id.list_item_yfname);
        }
        public void bind(String yfName){
            mYfNameTextView.setText(yfName);
        }
    }
    //---------------------------------------------------------------------------------
    public void setItemClickListener(RecycleViewOnItemClickListener listener){
        mItemClickListener = listener;
    }
    public void setItemLongClickListener(RecycleViewOnItemLongClickListener listener){
        mItemLongClickListener = listener;
    }
}
